<?php
namespace Admin\Controller;
use Think\Controller;
class ZyglController extends BaseController {

    public function zygl(){
        $roleid = session('roleid')+1;
        $did = session('did');
        $gid = session('gid');
        $sid = session('sid');
        $tid = session('tid');
        // echo $did,$sid,$gid,$tid;  1110
        $userid = session('userid');
        // echo session('userid');  4
        //取出未分配的资源条数
        $resourceModel = D('resource');
        //管理员
       if(session('roleid')==0){
           $sql = 'select count(id) as num from `cms_resource` where userid = '.$userid;
        }
        //总经理
        if(session('roleid')==1){
           $sql = 'select count(id) as num from `cms_resource` where  userid = '.$userid;
        }
        //总监
        if(session('roleid')==2)
        {
            $sql = 'select count(id) as num from `cms_resource` where userid = '.$userid;
        }
        //经理
        if(session('roleid')==3)
        {
            $sql = 'select count(id) as num from `cms_resource` where userid = '.$userid;
        }
        //主任
        if(session('roleid')==4)
        {
            $sql = 'select count(id) as num from `cms_resource` where userid = '.$userid;
        }
        //员工
        if(session('roleid')==5)
        {
            $sql = 'select count(id) as num from `cms_resource` where userid = '.$userid;
        }
        $count1 = $resourceModel->query($sql);
        //总监
        if(session('roleid')==2){
            $resourceInfo = $resourceModel->info();
        }
        //经理
        if(session('roleid')==3){
            $resourceInfo = $resourceModel->info();
        }
        //主任
        if(session('roleid')==4)
        {
            $resourceInfo = $resourceModel->info();
        }
        if(session('roleid')==5)
        {
            $resourceInfo = $resourceModel->info();
        }

        //取出当前用户下级用户
        $userModel = D('user');
        //管理员、经理、主任
        if(session('roleid')==0){
            $user = $userModel->where(array('roleid' =>$roleid , ))->select();
        }
        if(session('roleid')==1){
            $user = $userModel->where(array('roleid' =>$roleid ,'did' =>$did ))->select();
        }
        if(session('roleid')==2){
            $user = $userModel->where(array('roleid' =>$roleid ,'did' =>$did,'sid' =>$sid))->select();
        }
        if(session('roleid')==3){
            $user = $userModel->where(array('roleid' =>$roleid ,'did' =>$did,'gid' =>$gid,'sid'=>$sid))->select();
        }
        if(session('roleid')==4){
            $user = $userModel->where(array('roleid'=>$roleid,'did'=>$did,'gid'=>$gid,'sid'=>$sid,'tid'=>$tid))->select();
        }
            $this->assign(array(
              'count' =>$count1[0]['num'] , 
              'user' => $user,
              'resourceInfo' => $resourceInfo['data'],
              'count1' => $resourceInfo['count'],
              'pagecount' => $resourceInfo['count'],
              'page' =>$resourceInfo['page'],
              'resPage' =>$resourceInfo['resPage'],
            ));
        
        
    	$this->display();
    }
    //分配资源
    public function  allocate(){
        //接受分配数据
    
        $userId = I('post.userid');
        $totalCounts =  I('post.count');
        //取出分配人员信息
        $userModel = D('user');
        $user = $userModel->find($userId);
        $did=$user['did'];
        $gid=$user['gid'];
        $userid=$user['userid'];
        $updatetime=time();
         //进行分配
         $resourceModel = D('resource');
         $cou = floor($totalCounts/100000);
         $yushu = $totalCounts-($cou*100000);
         if($yushu){
             $num=$cou+1;
         }else{
             $num=$cou;
         }
         for ($i=1; $i <= $num; $i++) { 
            if($i==$num && $yushu!=""){
                $totalCounts= $yushu;
            }else{
                 $totalCounts=100000;
            }
            $sql = "select (id) from `cms_resource` where userid = ".session('userid')." and sid = 0 limit 0,".$totalCounts;
          
            $updateCount = $resourceModel->query($sql);
            $arr = array();
            foreach ($updateCount as $k=>$r) {
                foreach ($r as $k => $v) {
                    $arr[] = $v;
                }
            }

            $exp = implode(',',$arr);
            $sql = "UPDATE `cms_resource` SET userid='".$userid."',updatetime='".$updatetime."' WHERE id in (".$exp.");";

            $res = $resourceModel->query($sql);  
        }

        if($res!==FALSE){
            echo json_encode(array('ok' =>1, ));
        }else{
            echo json_encode(array('ok' =>2, ));
        }
    }
    //删除资源
    public function delete(){
        $statusId = $_POST['statusId'];
        $resourceModel = D('resource');
        //判断状态
        //意向客户
        if($statusId==3){
             $sql = "select (id) from `cms_resource` where sid in ('6,7,8,9')";
        }else{
             $sql = "select (id) from `cms_resource` where sid = ".$statusId;
        }
        //判断有没有
        $id = $resourceModel->query($sql);
        $count = count($id);
        if(!$count){
            echo json_encode(array('ok' =>0 , ));
            exit;
        }
         $cou = floor($count/100000);
         $yushu = $count-($cou*100000);
         if($yushu){
             $num=$cou+1;
         }else{
             $num=$cou;
         }
         for ($i=1; $i <= $num; $i++) { 
            if($i==$num && $yushu!=""){
                $count= $yushu;
            }else{
                 $count=100000;
            }
           $start = ($i-1)*100000;
           $shuju = array_slice($id,$start,$count);
           $arr = array();
           foreach ($shuju as $k=>$r) {
                foreach ($r as $k => $v) {
                    $arr[] = $v;
                }   
            }  
            $exp = implode(',',$arr);
            $sql1 = "delete from `cms_resource` where id in (".$exp.");";
            $res = $resourceModel->query($sql1);
        }
        
        if(empty($res)){
            echo json_encode(array('ok' =>1 , ));
        }else{
            echo json_encode(array('ok' =>2 , ));
        }
    }

   //去重
    public function quchong(){
        $resourceModel = D('resource');
        //判断有没有重复的资源
        //取出相同的pnumber的最大id的个数
        $sql = 'select min(id) id from `cms_resource` group by pnumber';
        $id = $resourceModel->query($sql);
        $arr = array();
        foreach ($id as $k => $v) {
            foreach ($v as $k1 => $v1) {
                $arr[] = $v1;
            }
        }
        $exp = implode(',', $arr);
        //在取出不在$exp里的id，删除
        $res = $resourceModel->where(array('id' =>array('not in' , $exp)))->field('id')->select();

        $arr1 = array();
        foreach ($res as $k => $v) {
            foreach ($v as $k1 => $v1) {
                $arr1[] = $v1;
            }
        }
         $exp1 = implode(',', $arr1);

        //如果不相等就删除
        if ($res) {
            $sql1 = "delete from `cms_resource` where id in (".$exp1.")";
            $del = $resourceModel->query($sql1);

           if(empty($del) && $del!==FALSE){
               echo json_encode(array('ok' =>1 , ));
           }else{
               echo json_encode(array('ok' =>2 , ));
           }
        }else{
            echo json_encode(array('ok' =>0 , ));
        }
    }


    //修改资源
    public function update(){
        $data['id'] = I('post.resourceId');
        $data['sid'] = I('post.statusId');
        $data['wechatname'] = i('post.wechatName');
        $data['updatetime'] = time();
        $resourceModel = D('resource');
        $res = $resourceModel->save($data);
        if($res!==FALSE){
            echo json_encode(array('ok' => 1, ));
        }else{
            echo json_encode(array('ok' => 2, ));
        }
    }
   //资源使用情况
    public function zysyqk(){
        //查询是的sid
        //区部对应有问题 注意看对应关系
        $userid = session('userid');
        $resourceModel = D('resource');
        $statusId = I('get.statusId');
        $ab = I('get.groupId');
        $a = I('get.buid');
        $abc = I('get.zuid');
        $abcd = I('get.duiid');
        //员工资源使用情况
        if(session('roleid')==5)
        {
            $page =I('get.page');
             if(!$page){
                $page = 1;
            }
            $num = 8;
            $count = ($page-1)*$num;
            if(!empty($statusId)){
                $where['sid'] = $statusId;
            }
            $where['userid'] = $userid;

             //分页
            // dump($where);
            $resCount = $resourceModel->where($where)->count();
             //总页数
            if($resCount==1){
                $resPage =1;
            }else{
                $resPage = ceil($resCount/$num);
            }
            //取出资源  
            $resourceInfo = $resourceModel->where($where)->limit($count , $num)->select();

            
        }
        //主任和员工资源使用情况
        // if (session('roleid')==4){
        //     $resourceInfo = $resourceModel->zysyqk($sid);
        // }
        // if(session('roleid')==5)
        // {
        //     $resourceInfo = $resourceModel->zysyqk($sid);
        // }

        //总经理使用资源情况
        if(session('roleid')==1 || session('roleid')==0 ||session('roleid')==2 || session('roleid')==3 ||session('roleid')==4){
        $resourceInfo = $resourceModel->resInfo();
       }
       //员工资源使用情况
       // if(session('roleid')==5)
       // {
       //  $resourceInfo = $resourceModel->resInfo();
       // }
       //经理查看分组
       // dump($resourceInfo);
       $groupModel = D('group');
       $group = $groupModel->where(array('did' =>'1' , ))->select();
       // dump($resourceInfo);
        $this->assign(array(
            'resourceInfo' =>$resourceInfo['data'],
            'count' => $resourceInfo['count'],
            'page' =>$resourceInfo['page'],
            'resPage' =>$resourceInfo['resPage'],
            'resCount' =>$resourceInfo,
            'group' => $group,
            'resourceInfo1'=>$resourceInfo,
            'sid'=>$sid,
            'page1' => $page,
            'count1' => $resCount,
            'resPage1' =>$resPage,
            'zz'=>$statusId,
            'buid'=>$ab,
            'quid' =>$a,
            'zuid'=>$abc,
            'duiid'=>$abcd,
            ));
        $this->display();
    }
    //根据资源号查询员工
    public function checkYuangong(){
            $pnumber = I('post.pnumber');
            $resourceModel = D('resource');
            // 根据号码取出一行数据
            $res = $resourceModel->where(array('pnumber' => $pnumber, ))->find();
            //对数据进行判断
            if(empty($res)){
                echo json_encode(array('ok' =>0 , ));
                exit;
            }

            if($res['onjob']==1){
                  $username = '该员工已离职';
                  $group ='该员工已离职';
                  $department ='该员工已离职';
             }else{
                  $userModel = D('user');
                  //查询出user信息 
                  $sql = "select a.username,a.roleid,a.did,b.dname,c.gname from `cms_user` as a left join `cms_department` as b on a.did = b.did left join `cms_group` as c on a.gid = c.gid where a.userid = ".$res['userid'];
                  $user = $userModel->query($sql);
                 //管理员没有分配数据
                  if($user[0]['roleid']==0){
                      echo json_encode(array('ok' =>1 , ));
                      return false;
                  }elseif($user[0]['roleid']==1){
                      //经理没有分配数据
                       $departmentModel = D('department');
                       $department = $departmentModel->find($user[0]['did']);
                       echo json_encode(array('ok' =>2 ,'depar'=>$department['dname']));
                       return false;
                  }else{
                       //信息
                       $department = $user[0]['dname'];
                       $group = $user[0]['gname'];
                       $username = $user[0]['username'];
                    }
                 }

             echo json_encode(array(
                 'pnumber' =>$res['pnumber'], 
                 'updatetime' =>date('Y-m-d H:i:s',$res['updatetime']),
                 'status' =>$res['sid'],
                 'department' =>$department,
                 'group'=>$group,
                 'username'=>$username,
            ));
    }
}